package com.ctl.utils;

public class NumberUtil {
    public static byte[] int32ToBytes(int number) {
        //int numCopy = number;
        byte[] bs = new byte[4];
        byte a = (byte) (number >> 24);
        byte b = (byte) ((number & 0xff0000) >> 16);
        byte c = (byte) ((number & 0xff00) >> 8);
        byte d = (byte) (number & 0xff);
        bs[0] = a;
        bs[1] = b;
        bs[2] = c;
        bs[3] = d;
        return bs;
    }

    public static int bytesToInt32(byte[] bs) {
        if (bs == null || bs.length != 4) {
            System.err.println("传入数组长度不为4");
            return 0;
        }
        //获取最高八位
        int num1 = 0;
        num1 = (int) (Integer.valueOf(num1) ^ (int) bs[0]);
        num1 = num1 << 24;
        //获取第二高八位
        int num2 = 0;
        num2 = (int) (Integer.valueOf(num2) ^ (int) bs[1]);
        num2 = num2 << 16;
        //获取第二低八位
        int num3 = 0;
        num3 = (int) (Integer.valueOf(num3) ^ (int) bs[2]);
        num3 = num3 << 8;
        //获取低八位
        int num4 = 0;
        num4 = (int) (Integer.valueOf(num4) ^ (int) bs[3]);
        return num1 ^ num2 ^ num3 ^ num4;
    }

    public static void main(String[] args) {
        int i=127;
        System.out.println(int32ToBytes(i));
        System.out.println(bytesToInt32(int32ToBytes(i)));
    }
}
